package com.lightsaber.trade.monitorclient.service.device;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springside.modules.utils.spring.SpringContextHolder;

import com.lightsaber.trade.cloth.ws.api.device.InfoCollectForDeviceDto;
import com.lightsaber.trade.cloth.ws.api.device.ViewCommand;

@Service
public class CollectInfoForDeviceClientManager implements Runnable {

    final static Logger LOG = LoggerFactory.getLogger(CollectInfoForDeviceClientManager.class);

    @Value(value = "#{appProperties['device.serial.number']}")
    private String deviceSerialNumber;

    public void searchCommand() {
        ViewCommand viewCommand = SpringContextHolder.getBean("viewCommand");
        InfoCollectForDeviceDto infoCollectForDeviceDto = null;

        try {
            infoCollectForDeviceDto = viewCommand.searchCommand(deviceSerialNumber);

        } catch (Exception e) {
            LOG.error("Request failed, Caused by: {}", e.getMessage());
            return;
        }

        if (infoCollectForDeviceDto.getInfoCollectForDeviceList() == null) {
            LOG.error("No command has been retried!");
            return;
        }

        for (InfoCollectForDeviceDto.InfoCollectForDevice infoCollectForDevice : infoCollectForDeviceDto
                .getInfoCollectForDeviceList()) {

            LOG.info(infoCollectForDevice.getOperator() + " say: "
                    + infoCollectForDevice.getContent());
        }

    }

    @Override
    public void run() {

        try {
            // ----------input your content on you keyboard---------START
            while (true) {

                // ----------input your content on you keyboard---------START
                try {
                    searchCommand();
                } catch (Exception e) {
                    LOG.error(e.getMessage(), e);
                }

                Thread.sleep(3000);
                // ----------input your content on you keyboard---------END

            }
            // ----------input your content on you keyboard---------END

        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }

    }
}
